package com.hyx.java.ctrl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyx.java.base.BaseCtrl;
import com.hyx.java.base.BaseResult;
import com.hyx.java.dto.HyxLoginReq;
import com.hyx.java.dto.HyxLoginResp;
import com.hyx.java.entity.HyxAdmin;
import com.hyx.java.entity.HyxUser;
import com.hyx.java.mapper.HyxAdminMapper;
import com.hyx.java.mapper.HyxUserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/")
@CrossOrigin(origins = "*")
@Api(value = "用户登录",tags = "用户登录")
public class HyxLoginCtrl extends BaseCtrl {

    @Autowired
    private HyxUserMapper userMapper;

    @Autowired
    private HyxAdminMapper adminMapper;
    @ApiOperation(value = "app端 登录")
    @RequestMapping(value = "app/login", method = RequestMethod.POST)
    public BaseResult<HyxLoginResp> userLogin(@RequestBody HyxLoginReq login) {
        if (login == null || StrUtil.isEmpty(login.getUserName()) || StrUtil.isEmpty(login.getPassword())){
            return BaseResult.fail("011", "指定参数为空");
        }
        HyxUser user = userMapper.selectOne(new QueryWrapper<HyxUser>().lambda().eq(HyxUser::getUserName, login.getUserName()));
        if (user == null) {
            return BaseResult.fail("012", "账号或密码错误");
        }
        if (!user.getPassword().equals(DigestUtil.md5Hex(login.getPassword()))) {
            return BaseResult.fail("012", "账号或密码错误");
        }
        String token = IdUtil.getSnowflake(1L, 6L).nextIdStr();
        setCache(token, JSONUtil.toJsonStr(user));
        return BaseResult.success(HyxLoginResp.builder().token(token).userName(login.getUserName()).build());
    }

    @ApiOperation(value = "web端 登录")
    @RequestMapping(value = "admin/login", method = RequestMethod.POST)
    public BaseResult<HyxLoginResp> adminLogin(@RequestBody HyxLoginReq login) {
        if (login == null || StrUtil.isEmpty(login.getUserName()) || StrUtil.isEmpty(login.getPassword())){
            return BaseResult.fail("011", "指定参数为空");
        }
        HyxAdmin admin = adminMapper.selectOne(new QueryWrapper<HyxAdmin>().lambda().eq(HyxAdmin::getUserName, login.getUserName()));
        if (admin == null) {
            return BaseResult.fail("012", "账号或密码错误");
        }
        if (!admin.getPassword().equals(DigestUtil.md5Hex(login.getPassword()))) {
            return BaseResult.fail("012", "账号或密码错误");
        }
        String token = IdUtil.getSnowflake(1L, 8L).nextIdStr();
        setCache(token, JSONUtil.toJsonStr(admin));
        return BaseResult.success(HyxLoginResp.builder().token(token).userName(login.getUserName()).build());
    }
    @ApiOperation(value = "app端 注销")
    @RequestMapping(value = "app/login-out", method = RequestMethod.POST)
    public BaseResult<String> userLoginOut() {
        delCache();
        return BaseResult.success();
    }
    @ApiOperation(value = "web端 注销")
    @RequestMapping(value = "admin/login-out", method = RequestMethod.POST)
    public BaseResult<String> adminLoginOut() {
        delCache();
        return BaseResult.success();
    }
}
